//
//  HMtabBarView.swift
//  Weibo
//
//  Created by 京城 on 16/7/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMtabBarView: UITabBar {
    
    var callback:(() -> Void)?
    
    //懒加载一个button
    lazy var composeButton : UIButton = {
        
        let btn =  UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted);
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
         self.addSubview(btn)

        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    
    //composeClick 被点击
    func composeClick() {
        
        callback?()
        

    }
    
    
    override func layoutSubviews() {
       
        let count = 5
        let newWidth = self.frame.width / CGFloat(count)
        
        var index = 0
        for item in self.subviews{

            let cls = NSClassFromString("UITabBarButton")!
            
            if(item.isKindOfClass(cls)){
                
                item.frame = CGRectMake(CGFloat(index) * newWidth, 0, newWidth, self.frame.height)
                
                index++
                
                if(index == 2){
                    index++
                }
                
            }
           
        }
        
        composeButton.frame = CGRectMake(2 * newWidth, 0, newWidth, self.frame.height)
        self.backgroundColor = UIColor(patternImage: UIImage(named: "tabbar_background")!)
    }
    
    
}

